package com.zy.qudadid.ui.adapter;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.zy.lcpassenger.R;
import com.zy.qudadid.model.StrokeInfor;
import com.zy.qudadid.ui.view.base.OnItemClickListener;
import com.zy.qudadid.utils.TimeUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by ninos on 2016/9/20.
 */
public class IndexPublishAdapter extends RecyclerView.Adapter<IndexPublishAdapter.VHolder> {

    ArrayList<StrokeInfor> data = new ArrayList<>();
    Context context;
    StrokeInfor stroke;

    public IndexPublishAdapter(Context context, ArrayList<StrokeInfor> data) {
        this.context = context;
        this.data = data;
    }

    @Override
    public VHolder onCreateViewHolder(ViewGroup parent, int viewType)
    {
        VHolder holder = new VHolder(LayoutInflater.from(
                context).inflate(R.layout.activity_index_xingcheng_item, parent,
                false));
        return holder;
    }

    @Override
    public int getItemCount()
    {
        return data.size();
    }

    @Override
    public void onBindViewHolder(VHolder holder, final int position)
    {

        if (mOnItemClickListener != null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(view,  position , data.get(position));
                }
            });

        } else {
            holder.itemView.setOnClickListener(null);
        }


        stroke =(StrokeInfor) data.get(position);
        String date = stroke.yuyueche_departtime;
        String[] str=null;
        if(date==null) {

        }else{
            str = date.split(" ");
            date = TimeUtil.getDay(str[0]);
        }

        if(stroke.state.equals("1")){ // 已接单
            holder.txtState.setText("已接单");
        }else if(stroke.state.equals("2")){ // 已上车
            holder.txtState.setText("已上车");
        }else if(stroke.state.equals("6")){ // 待评价
            holder.txtState.setText("待评价");
        }else if(stroke.state.equals("5")){ // 待支付
            holder.txtState.setText("待支付");
        }else if(stroke.state.equals("0")){ // 未接单
            holder.txtState.setText("等待司机接单");
        }else if(stroke.state.equals("7")){
            holder.txtState.setText("等待司机确认支付");
        }else if(stroke.state.equals("8")){
            holder.txtState.setText("司机拒绝现金支付，请重新支付");
        }

        holder.txtDate.setText(date);
        if(date == null){

        }else {
            holder.ixi_time.setText(str[1].substring(0, str[1].lastIndexOf(":")));
        }
        holder.ixi_adds.setText("到达地："+stroke.destination);
    }

    class VHolder extends RecyclerView.ViewHolder {

        @Nullable @Bind(R.id.ixi_adds)TextView ixi_adds;
        @Nullable @Bind(R.id.ixi_time)TextView ixi_time;
        @Nullable @Bind(R.id.txtDate) TextView txtDate;
        @Nullable @Bind(R.id.txtState) TextView txtState;

        VHolder(View view) {
            super(view);
            ButterKnife.bind(this, view);
        }
    }

    /**
     * Item点击事件
     */
    public OnItemClickListener mOnItemClickListener;
    /**
     * 设置监听者
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }
}